﻿using Application.Interfaces.Caching;
using Application.Interfaces.Repository;
using Application.Services.Interfaces;
using Domain.Models.Settings;
using System.Linq;
using System.Security.Permissions;

namespace Application.Services.Implementations
{
    public class AdminService : IAdminService
    {
        private ICacheManager _cacheManager;
        private IRepository<Setting> _settingRepo;

        public AdminService(ICacheManager cacheManager, IRepository<Setting> settingRepo)
        {
            _cacheManager = cacheManager;
            _settingRepo = settingRepo;
        }

        [PrincipalPermission(SecurityAction.Demand, Role = "admin")]
        public Setting GetSetting()
        {
            return _settingRepo.Find().FirstOrDefault();
        }

        [PrincipalPermission(SecurityAction.Demand, Role = "admin")]
        public void UpdateSetting(Setting setting)
        {
            if (_settingRepo.SaveChanges() > 0)
            {
                _cacheManager.Remove(CachingKeys.Setting.ToString());
            }
        }
    }
}